#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTableView>
#include <QStandardItemModel>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::findAllFiles(const QString path, QFileInfoList &fileInfoList)
{
    QDir dir(path);
    foreach(QFileInfo info,dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Dirs)){
        qDebug()<<"dir:"<<info.filePath();
        findAllFiles(info.filePath(),fileInfoList);
    }

    foreach(QFileInfo info,dir.entryInfoList(QDir::Files)){
        fileInfoList.append(info);
        qDebug()<<"fileInfo"<<info;
    }
}


void MainWindow::on_pushButton_clicked()
{
    m_fileInfoList.clear();

    findAllFiles(ui->lineEdit->text(),m_fileInfoList);



    QAbstractItemModel *pModel = ui->tableView->model();
    if(!pModel){
        pModel->deleteLater();
        pModel = nullptr;
    }

    QStandardItemModel *pStandModel = new QStandardItemModel(m_fileInfoList.count(),3);



    for (int row = 0; row < pStandModel->rowCount(); ++row) {

        QStandardItem *item = new QStandardItem(m_fileInfoList[row].fileName());
        pStandModel->setItem(row, 0, item);

        QStandardItem *item1 = new QStandardItem(m_fileInfoList[row].absolutePath());
        pStandModel->setItem(row, 1, item1);

        QStandardItem *item2 = new QStandardItem(QString::number(m_fileInfoList[row].size()));
        pStandModel->setItem(row, 2, item2);

    }

    ui->tableView->setModel(pStandModel);
}

